package com.example.bdmanagerapplication

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.bdmanagerapplication.MyUtils.launch
import kotlinx.coroutines.launch

class PoiInfoViewModel:BaseViewModel() {

    fun addPoi(poiInfoDao: PoiInfoDao,poi:PoiEntity){
        launch {
            val result = poiInfoDao.insertPoiInfo(poi)

        }
    }
    private val mGetAllPoiResult = MutableLiveData<List<PoiEntity>>()
    val getPoiResult: LiveData<List<PoiEntity>> get() = mGetAllPoiResult
    fun getAllPoi(poiInfoDao: PoiInfoDao) {
        launch {
            val poiList = poiInfoDao.loadAllPoi()
            mGetAllPoiResult.postValue(poiList) // 使用 postValue 更新
        }
    }

    // 检查 POI 是否存在


    fun isPoiExists(uid: String,poiInfoDao: PoiInfoDao) :Int{

            return if (poiInfoDao.existsByUid(uid) > 0) 1 else 0


    }
    private val mIsPoiExistsResult = MutableLiveData<List<MyPoiInfo>>()
    val isPoiExistsResult: LiveData<List<MyPoiInfo>> get() = mIsPoiExistsResult
    fun getChangeList(poiInfoDao: PoiInfoDao,list:List<MyPoiInfo>){
        launch {
            val tempList=ArrayList<MyPoiInfo>()
            list.forEach {
                Log.d("测试数据111","${isPoiExists(it.uid,poiInfoDao)}")
                it.isClient=isPoiExists(it.uid,poiInfoDao)
                tempList.add(it)
            }
            mIsPoiExistsResult.postValue(tempList)
        }


    }
}